package com.bocaizp.operation.service.impl;

import com.bocaizp.common.constant.MessageConstant;
import com.bocaizp.common.context.BaseContext;
import com.bocaizp.common.exception.DataBaseErrorException;
import com.bocaizp.operation.mapper.ChatMapper;
import com.bocaizp.operation.service.ChatService;
import com.bocaizp.pojo.DTO.ChatRecordDTO;
import com.bocaizp.pojo.VO.AddFriendVO;
import com.bocaizp.pojo.VO.FriendListVO;
import com.bocaizp.pojo.VO.OtherInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

/**
 * Author : huang jie
 * Date : 2024/5/9 23:57
 **/
@Service
@Slf4j
public class ChatServiceImpl implements ChatService {

	@Autowired
	private ChatMapper chatMapper;

	/**
	 * 获取好友列表
	 * @return
	 */
	public List<FriendListVO> getfriend() {
		Long id = BaseContext.getCurrentId();
		List<FriendListVO> friendListVOS = chatMapper.getuserOne(id);
		for (FriendListVO f : friendListVOS){
			if(!Objects.equals(f.getOwnId(), id)){
				f.setId(f.getOwnId());
			}
			FriendListVO friendListVO = chatMapper.getinfo(f.getId());
			f.setName(friendListVO.getName());
			f.setLogo(friendListVO.getLogo());
			f.setCompany(friendListVO.getCompany());
			f.setBossJob(friendListVO.getBossJob());
			String content = chatMapper.getContent(id, f.getId());
			f.setContent(content);
			String time = computedTime(f.getLastTime());
			f.setLastTime(time);
		}
		return friendListVOS;
	}

	/**
	 * 上次聊天日期
	 * @param time
	 * @return
	 */
	public String computedTime(String time){
		time = time.replace(" ","T");
		System.out.println(time);
		LocalDateTime datetime = LocalDateTime.parse(time);
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		String date1 = datetime.format(formatter);
		LocalDate date = LocalDate.parse(date1);
		LocalDate today = LocalDate.now();
		if(date.equals(today)){
			DateTimeFormatter form = DateTimeFormatter.ofPattern("HH:mm");
			return datetime.format(form);
		} else {
			LocalDate yesterday = today.minusDays(1);
			if(date.equals(yesterday)){
				return MessageConstant.YESTERDAY;
			} else if (today.minusDays(7).isBefore(date)) {
				// 获取星期几
				DayOfWeek dayOfWeek = date.getDayOfWeek();
				// 使用DayOfWeek的枚举值名称
				return dayOfWeek.getDisplayName(TextStyle.FULL, Locale.SIMPLIFIED_CHINESE);
			} else {
				DateTimeFormatter form1 = DateTimeFormatter.ofPattern("MM-dd");
				return date.format(form1);
			}
		}
	}


	/**
	 * 获取聊天历史
	 * @param id
	 * @return
	 */
	public OtherInfoVO getHistory(String id) {
		Long ownId = BaseContext.getCurrentId();
		List<ChatRecordDTO> chatRecordDTOS = chatMapper.getHistory(ownId, id);
		System.out.println(chatRecordDTOS);
		for (ChatRecordDTO c : chatRecordDTOS){
			if(Objects.equals(c.getOwnId(), ownId)){
				if(c.getStatus() == 1){
					c.setFlag(2);
				}else{
					c.setFlag(1);
				}
			}else{
				if(c.getStatus() == 1){
					c.setFlag(3);
				}else {
					c.setFlag(0);
				}
			}
		}
		OtherInfoVO otherInfoVO = chatMapper.getotherInfo(id);
		otherInfoVO.setHistory(chatRecordDTOS);
		return otherInfoVO;
	}

	/**
	 * 存储聊天记录
	 *
	 * @param toName
	 * @param mess
	 * @param flag
	 */
	public void insertRecord(String sid, String toName, String mess, int flag) {
		log.info("进行数据整理");
		Long id = Long.valueOf(sid);
		Long rid = Long.valueOf(toName);
		int res;
//		设置状态 1：表示简历 0：表是普通消息
		if(flag == 3){
			res = 1;
		}else{
			res = 0;
		}
		chatMapper.insert(id, rid,mess,res);
		LocalDateTime localDateTime = LocalDateTime.now();
		chatMapper.updateTime(id, rid, localDateTime);

	}

	/**
	 * 搜索好友
	 * @return
	 */
	public List<AddFriendVO> addFriendList(String username) {
		List<AddFriendVO> addFriendVOList = chatMapper.addFriendList(username);
		//如果存在自己id，则一处那组数据
		Long id = BaseContext.getCurrentId();
		for (AddFriendVO a : addFriendVOList){
			if(Objects.equals(a.getId(), id)){
				addFriendVOList.remove(a);
				break;
			}
		}
		return addFriendVOList;
	}

	/**
	 * 添加好友
	 * @param id
	 * @return
	 */
	public void build(Long id) {
		Long ownId = BaseContext.getCurrentId();
		try {
			chatMapper.build(ownId,id);
		} catch (Exception e) {
			throw new DataBaseErrorException("添加失败！");
		}
	}

	/**
	 * 删除好友
	 * @param id
	 */
	public void delete(Long id) {
		Long ownId = BaseContext.getCurrentId();
		chatMapper.delete(ownId,id);
		chatMapper.deleteRecords(ownId,id);
	}

}
